<?php


	fta_registerModules(
		array(
			"Zend/XmlRpc/Client"
		)
	);
	
	class FTAWordPress extends FTAObject {
		
	//	protected $user;
	//	protected $password;
		protected $xmlrpcurl;
		
		protected $_xmlrpcclient;					
		
		//list of all methods that we can magically call..
		static $callableMethods = array(
//wordpress specific api calls		

// WordPress API
'wp.getUsersBlogs'		,
'wp.getPage'			,
'wp.getPages'			,
'wp.newPage'			,
'wp.deletePage'			,
'wp.editPage'			,
'wp.getPageList'		,
'wp.getAuthors'			,
'wp.getCategories'		,
'wp.getTags'			,
'wp.newCategory'		,
'wp.deleteCategory'		,
'wp.suggestCategories'	,
'wp.uploadFile'			,
'wp.getCommentCount'	,
'wp.getPostStatusList'	,
'wp.getPageStatusList'	,
'wp.getPageTemplates'	,
'wp.getOptions'			,
'wp.setOptions'			,
'wp.getComment'			,
'wp.getComments'		,
'wp.deleteComment'		,
'wp.editComment'		,
'wp.newComment'			,
'wp.getCommentStatusList' ,

// Blogger API
'blogger.getUsersBlogs' ,
'blogger.getUserInfo' ,
'blogger.getPost' ,
'blogger.getRecentPosts' ,
'blogger.getTemplate' ,
'blogger.setTemplate' ,
'blogger.newPost' ,
'blogger.editPost' ,
'blogger.deletePost' ,

// MetaWeblog API (with MT extensions to structs)
'metaWeblog.newPost' ,
'metaWeblog.editPost' ,
'metaWeblog.getPost' ,
'metaWeblog.getRecentPosts' ,
'metaWeblog.getCategories' ,
'metaWeblog.newMediaObject' ,

// MetaWeblog API aliases for Blogger API
// see http://www.xmlrpc.com/stories/storyReader$2460
'metaWeblog.deletePost' ,
'metaWeblog.getTemplate' ,
'metaWeblog.setTemplate' ,
'metaWeblog.getUsersBlogs' ,

// MovableType API
'mt.getCategoryList' ,
'mt.getRecentPostTitles' ,
'mt.getPostCategories' ,
'mt.setPostCategories' ,
'mt.supportedMethods' ,
'mt.supportedTextFilters' ,
'mt.getTrackbackPings' ,
'mt.publishPost' ,

// PingBack
'pingback.ping' ,
'pingback.extensions.getPingbacks' ,

'demo.sayHello' ,
'demo.addTwoNumbers', 
'wp.iDuzNotEggsists' 
		);
		 
		public function __construct($user,$password,$xmlrpcurl){
			parent::__construct();
			
			$this->user = $user;
			$this->password = $password;
			$this->xmlrpcurl = $xmlrpcurl;
			
			
		}
		
		
		public function getXMLRPCCLient(){
			if (is_null($this->_xmlrpcclient)){
				$this->_xmlrpcclient = new Zend_XmlRpc_Client($this->xmlrpcurl);
				$this->_xmlrpcclient->setSkipSystemLookup(true);
				$client = $this->_xmlrpcclient->getHttpClient();
				
		//		$client->setHeaders('accept-encoding',"identity;q=0");
				
				$this->_xmlrpcclient->setHttpClient($client);
			}
			return $this->_xmlrpcclient;
		}
		
		
		
		public function __call( $name , $arguments){
        	
			if (method_exists($this,$name) ){
				return call_user_func_array(array($this,$name), $arguments);	
			} 
			
			//wordpress rpc call
			$wpMethodName = strtr($name,'_','.');
			if (in_array($wpMethodName,static::$callableMethods)){
				//$arguments = array_merge($arguments, array($this->user,$this->password));
				//print_r( $arguments );
				return $this->XMLRPCCLient->call($wpMethodName,$arguments);
			}
			
			
			
			$trace = debug_backtrace();
        	trigger_error(
            'Undefined method via __call(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        	return null;			
		}
	}